import { defineConfig, loadEnv } from 'vite';
import vue from '@vitejs/plugin-vue';
import AutoImport from 'unplugin-auto-import/vite';
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import { resolve } from 'path';

// const devDomain = 'serve本地运行接口地址';
const devDomain = 'https://local-network-demo.com';

export default ({ mode }) => {
  process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };
  return defineConfig({
    base: './',
    server: {
      open: true,
      proxy: {
        '^/admin-demo': {
          target: process.env.VITE_BASE_URL || devDomain,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/admin-demo/, '/admin-demo'),
        },
      },
      host: '0.0.0.0',
    },
    css: {
      preprocessorOptions: {
        scss: {
          charset: false,
        },
      },
    },
    plugins: [
      vue(),
      AutoImport({
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        resolvers: [ElementPlusResolver()],
      }),
    ],
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
        __V: resolve(__dirname, './src/views'),
        __C: resolve(__dirname, './src/components'),
        __A: resolve(__dirname, './src/assets'),
        __M: resolve(__dirname, './src/model'),
      },
    },
  });
};
